<?php

require_once "Basic.php";

class FrotaDisponivelMarca extends BasicEntity {
  
  public $list;
  
  public function __construct() {
    $this->list = new Report();
    $this->fields = array (
      new FormField(true, "iniPeriod", htmlentities("Período de Início"), new FormFieldDateHourMinute()),
      new FormField(true, "endPeriod", htmlentities("Período de Início"), new FormFieldDateHourMinute()),
    );
    $this->processFields();
  }
  
  public function actionDescription() {
    return "CalcFrota";
  }
  
  public function actionCalcFrota() {
    foreach($this->fields as $k => $v) {
      $$k = $v->value();
    }
    
    $filial = intval(userId());
    $iniPeriod = FormFieldDateHourMinute::toDateTime($iniPeriod);
    $endPeriod = FormFieldDateHourMinute::toDateTime($endPeriod);
    
    $db = new DBUtils();
    $db->connect();
    $db->runQuery("select * from FrotaDisponivelMarca(?,?,?)", array($iniPeriod, $endPeriod, $filial));
    for($item = $db->getItem(); $item !== null; $item = $db->getItem()) {
      $qtd = $item->quantidadeTotal - $item->quantidadeAlocada - $item->quantidadeReservada;
      if($qtd > 0) {
        $listItem = new ReportItem("-");
        $listItem->addLine("Marca", $item->marca);
        $listItem->addLine("Quantidade", $qtd);
        $this->list->addItem($listItem);
      }
    }
    
    return true;
  }
  
  public function getListHtml() {
    return $this->list->getHtml();
  }
  
}

mustBeEmployee();
correctStdFormGET();

$page = new Page(htmlentities("Frota Disponível por Marca Na Filial"));
$porMarca = new FrotaDisponivelMarca();
$form = new StdForm($porMarca, $_GET, $_POST, "", "", "FrotaDisponivelMarca.php");
if($form->processForm() && $form->receiving()) {
  $page->addContent($porMarca->getListHtml());
}
else {
  $page->addContent($form->getHtml());
}

echo $page->getHtml();

